#ifdef MSVC

#endif

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QDebug>

#include<QStandardPaths>


MainWindow::MainWindow(QWidget *parent) :
    //
    QMainWindow(parent,Qt::WindowCloseButtonHint|Qt::WindowMinimizeButtonHint),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //设置进度条的最小值
     ui->seekbar->setMaximum(0);

     // 设置窗口的标题
    this->setWindowTitle(QString("music_player"));


    //this->setObjectName("MainWindow");

    playList->addMedia(QMediaContent(QMediaResource(QUrl("C:/Users/cseyh/Qt_p/music_player/res/mp3/hongyangjing.mp3"))));


    playList->addMedia(QMediaContent(QMediaResource(QUrl("C:/Users/cseyh/music/ceshi.mp3"))));

    playList->setCurrentIndex(1);

    player->setPlaylist(playList);

    //设置播放模式
    playList->setPlaybackMode(QMediaPlaylist::Loop);
    player->play();

    //
    qDebug()<<QStandardPaths::writableLocation(QStandardPaths::MusicLocation);



    // 获取默认的音乐文件夹
    auto musicHome = QStandardPaths::writableLocation(QStandardPaths::MusicLocation);

    qDebug()<<"默认的音乐文件夹"<<musicHome;

    connect(ui->btn_pre,SIGNAL(clicked(bool)),this,SLOT(pre()));


    connect(ui->btn_pause_play,SIGNAL(clicked(bool)),this,SLOT(pause_play()));

    connect(ui->btn_next,SIGNAL(clicked(bool)),this,SLOT(next()));

    // 连接位置发生变化的信号
    connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(seekbarChange(qint64)));

    connect(player,SIGNAL(durationChanged(qint64)),this,SLOT(seekbarRangeMax(qint64)));



  //  connect(ui->seekbar,SIGNAL(valueChanged(int)),this,SLOT(playerPosition(int)));

    qDebug()<<"duration is "<<player->duration();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::pre()
{

    qDebug()<<"pre";
    auto list =  player->playlist();

    list->previous();



}

void MainWindow::next()
{
    qDebug()<<"next";
    auto list =     player->playlist();

    list->next();
}

void MainWindow::pause_play()
{
    qDebug()<<"pause play";
    auto st= player->state();



    //   QMediaPlayer::StoppedState
    //   QMediaPlayer::PlayingState
    //   QMediaPlayer::PausedState

    if(st == QMediaPlayer::PausedState){
        player->play();
    } else if (st ==QMediaPlayer::PlayingState ){
        player->pause();
    }
}

void MainWindow::seekbarChange(qint64 value)
{
     qDebug()<<"seekbarChange "<<value;
    int v = value;
    ui->seekbar->setValue(v);
}

void MainWindow::playerPosition(int value)
{
    qDebug()<<"playerPosition: "<<value;
   // player->setPosition(value);
}

void MainWindow::seekbarRangeMax(qint64 value)
{
    qDebug()<<"seekbarRangeMax "<<value;

    ui->seekbar->setMaximum(value);
}
